#include "login.h"


void login(int sockfd, Usr * usr){
    int epollfd = epoll_create(1);
    struct epoll_event event;
    event.data.fd = sockfd;
    event.events = EPOLLIN;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event);
    struct epoll_event ready_set[5];
    printf("into while\n");
    while(1){
        printf("注册y,登录n\n");
        char c;
        scanf("%c",&c);
        send(sockfd,&c,1,0);
        if(c == 'y'){//进入注册语句
            while(1){
                printf("已进入注册！\n");
                printf("请输入用户名！\n");
                scanf("%s",usr->usrname);
                printf("请输入密码！\n");
                scanf("%s",usr->password);
                // 清空输入缓冲区
                int ch;
                while ((ch = getchar()) != '\n');
                int length = sizeof(Usr); 
                send(sockfd,&length,sizeof(int),0);
                send(sockfd,usr,length,0);
                char signal = '\0';
                while(1){//等待服务器发送注册成功信号
                    int readynum = epoll_wait(epollfd,ready_set,5,-1);
                    for(int i = 0; i < readynum ; ++i){
                        if(ready_set->data.fd == sockfd){
                            recv(sockfd,&signal,1,0);
                            printf("signal = %c\n",signal);
                            if(signal == 'y'){
                                printf("注册成功!\n");
                            }
                            else{
                                printf("注册失败！\n");
                            }
                            break;
                        }
                    }
                    break;
                }
                break;
            }
        }
        else if(c == 'n'){//进入登录语句
            while(1){
                printf("已进login！\n");
                printf("请输入用户名！\n");
                scanf("%s",usr->usrname);
                printf("请输入密码！\n");
                scanf("%s",usr->password);
                int length = sizeof(Usr);
                send(sockfd,&length,sizeof(int),0);
                send(sockfd,usr,length,0);
                char signal2 = '\0';
                while(1){//等待服务器发送登录成功信号
                    int readynum = epoll_wait(epollfd,ready_set,5,-1);
                    for(int i = 0; i < readynum ; ++i){
                        if(ready_set->data.fd == sockfd){
                            recv(sockfd,&signal2,1,0);
                            printf("signal2 = %c\n",signal2);
                            if(signal2 == 'y'){
                                printf("登录成功!\n");
                            }
                            else{
                                printf("登录失败！\n");
                            }
                            break;
                        }
                    }
                    break;
                }            
                if(signal2 == 'y'){
                    break;//登录成功跳出登录语句
                }
                else{
                    continue;//登录失败继续循环
                }
            }
        }
        else{
            printf("非法字符，请重新输入\n");
            // 清空输入缓冲区
            int ch;
            while ((ch = getchar()) != '\n');
            continue;
        }
        if(c == 'y'){
            continue;
        }
        break;
    }

}
